//
//  GuideVeiw.m
//  基本框架
//
//  Created by runone02 on 2018/8/9.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "GuideVeiw.h"

@interface GuideVeiw()
/**图片 */
@property (nonatomic, strong) UIImageView *GuideImageView;
/** */
@property (nonatomic, strong) UIButton *GuideBtn;
@end

@implementation GuideVeiw


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {

    self.GuideImageView = [[UIImageView alloc]init];
    self.GuideImageView.userInteractionEnabled = YES;
    [self.GuideImageView setImage:IMAGE(@"组-35")];
    [self addSubview:self.GuideImageView];
    UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(guideButtonClick)];
    [self.GuideImageView addGestureRecognizer:labelTapGestureRecognizer];

    [self.GuideImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(self);
    }];
    
    self.GuideBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.GuideBtn.layer setMasksToBounds:YES];
    [self.GuideBtn.layer setCornerRadius:25.0];
    [self.GuideBtn.layer setBorderWidth:1.0];
    self.GuideBtn.titleLabel.font = TITLE_FONT(26);
    self.GuideBtn.layer.borderColor = [UIColor orangeColor].CGColor;
    
    [self.GuideBtn setTitle:@"我知道了" forState:UIControlStateNormal];
    [self.GuideBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.GuideBtn addTarget:self action:@selector(guideButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.GuideImageView addSubview:self.GuideBtn];
    
    [self.GuideBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.GuideImageView.mas_bottom).with.offset(-W_RATIO(50));
        make.right.mas_equalTo(self.GuideImageView.mas_right).with.offset(-W_RATIO(50));
        make.width.height.mas_equalTo(50);
    }];
}


- (void)guideButtonClick {
    // 1. 从父控件移除
    [self removeFromSuperview];
}
/**
 * 显示引导通知view
 */
+ (void)show{
    // 1. 获取当前版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    
    // 2. 获取偏好设置中版本号
    NSString *version = [[NSUserDefaults standardUserDefaults] stringForKey:@"version"];
    
    // 3. 判断当前版本号与偏好设置中版本号是否一致
    if (![currentVersion isEqualToString:version]) {
        // 4. 获取window
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        
        // 5. 将通知引导页添加到窗口上面
        GuideVeiw *gudieView = [[GuideVeiw alloc]init];
        gudieView.frame = window.bounds;
        [window addSubview:gudieView];
        
        // 6. 存储当前版本号
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:@"version"];
        
        // 7. 立即同步到偏好设置
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

@end
